# 完整重新测试脚本 - 带详细日志

Write-Host "=== Vulkan 性能分析 - 完整测试流程 ===" -ForegroundColor Cyan
Write-Host ""

# 步骤 1: 清理并重新编译
Write-Host "步骤 1/5: 清理并重新编译..." -ForegroundColor Yellow
Write-Host "  清理旧文件..." -ForegroundColor Gray
hvigorw clean

Write-Host "  重新编译..." -ForegroundColor Gray
hvigorw assembleHap

if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ 编译失败!" -ForegroundColor Red
    exit 1
}
Write-Host "✓ 编译成功" -ForegroundColor Green
Write-Host ""

# 步骤 2: 卸载并重新安装
Write-Host "步骤 2/5: 重新安装应用..." -ForegroundColor Yellow
Write-Host "  卸载旧版本..." -ForegroundColor Gray
hdc uninstall com.huawei.vulkantestapp10 2>$null

Write-Host "  安装新版本..." -ForegroundColor Gray
hdc install entry\build\default\outputs\default\entry-default-signed.hap

if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ 安装失败!" -ForegroundColor Red
    exit 1
}
Write-Host "✓ 安装成功" -ForegroundColor Green
Write-Host ""

# 步骤 3: 启动应用
Write-Host "步骤 3/5: 启动应用..." -ForegroundColor Yellow
hdc shell aa start -a EntryAbility -b com.huawei.vulkantestapp10

if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ 启动失败!" -ForegroundColor Red
    exit 1
}

Write-Host "✓ 应用已启动" -ForegroundColor Green
Write-Host ""
Write-Host "🎮 请在设备上操作应用 30 秒..." -ForegroundColor Cyan
Write-Host "   (旋转、缩放、移动等操作)" -ForegroundColor Gray
Write-Host ""

# 倒计时 30 秒
for ($i = 30; $i -gt 0; $i--) {
    Write-Progress -Activity "应用运行中" -Status "剩余时间: $i 秒" -PercentComplete ((30 - $i) / 30 * 100)
    Start-Sleep -Seconds 1
}
Write-Progress -Activity "应用运行中" -Completed

Write-Host ""

# 步骤 4: 停止应用
Write-Host "步骤 4/5: 停止应用..." -ForegroundColor Yellow
hdc shell aa force-stop com.huawei.vulkantestapp10

Write-Host "✓ 应用已停止" -ForegroundColor Green
Write-Host "  等待数据写入..." -ForegroundColor Gray
Start-Sleep -Seconds 3
Write-Host ""

# 步骤 5: 收集日志并查找文件
Write-Host "步骤 5/5: 收集诊断信息..." -ForegroundColor Yellow
Write-Host ""

# 5.1 收集应用日志
Write-Host "  5.1 收集应用日志..." -ForegroundColor Cyan
$logFile = "performance_logs.txt"
hdc shell "logcat -d -s PerformanceProfiler:V OH_LOG:V" > $logFile

if (Test-Path $logFile) {
    $logContent = Get-Content $logFile
    
    Write-Host ""
    Write-Host "=== 关键日志 ===" -ForegroundColor Yellow
    
    # 检查是否启用
    $enableLog = $logContent | Select-String "Enabling Performance"
    if ($enableLog) {
        Write-Host "✓ 性能分析器已启用" -ForegroundColor Green
        Write-Host "  $enableLog" -ForegroundColor Gray
    } else {
        Write-Host "✗ 未找到启用日志" -ForegroundColor Red
    }
    
    # 检查是否导出
    $exportLog = $logContent | Select-String "Exporting Performance|ExportToFile"
    if ($exportLog) {
        Write-Host "✓ 检测到导出操作" -ForegroundColor Green
        foreach ($line in $exportLog) {
            Write-Host "  $line" -ForegroundColor Gray
        }
    } else {
        Write-Host "✗ 未找到导出日志 (Cleanup 可能未被调用!)" -ForegroundColor Red
    }
    
    # 检查错误
    $errorLog = $logContent | Select-String "ERROR|Failed"
    if ($errorLog) {
        Write-Host "⚠ 发现错误信息:" -ForegroundColor Red
        foreach ($line in $errorLog) {
            Write-Host "  $line" -ForegroundColor Red
        }
    }
    
    Write-Host ""
    Write-Host "完整日志已保存到: $logFile" -ForegroundColor Cyan
}

Write-Host ""

# 5.2 查找文件
Write-Host "  5.2 查找性能数据文件..." -ForegroundColor Cyan

$paths = @(
    "/data/app/el2/100/base/com.huawei.vulkantestapp10/haps/entry/files/",
    "/data/local/tmp/",
    "/sdcard/",
    "/data/storage/el2/base/haps/entry/files/"
)

$found = $false
foreach ($path in $paths) {
    $files = hdc shell "ls $path 2>/dev/null" 2>$null
    if ($files -match "profiling") {
        Write-Host ""
        Write-Host "✓ 找到文件! 路径: $path" -ForegroundColor Green
        Write-Host "  文件列表:" -ForegroundColor Gray
        Write-Host "  $files" -ForegroundColor Cyan
        Write-Host ""
        Write-Host "拉取文件..." -ForegroundColor Yellow
        
        hdc file recv "${path}profiling_summary.csv" ".\oh_profiling_summary.csv"
        hdc file recv "${path}profiling_detailed.csv" ".\oh_profiling_detailed.csv"
        
        if (Test-Path "oh_profiling_summary.csv") {
            $size = (Get-Item "oh_profiling_summary.csv").Length
            Write-Host "✓ oh_profiling_summary.csv ($size bytes)" -ForegroundColor Green
        }
        
        if (Test-Path "oh_profiling_detailed.csv") {
            $size = (Get-Item "oh_profiling_detailed.csv").Length
            Write-Host "✓ oh_profiling_detailed.csv ($size bytes)" -ForegroundColor Green
        }
        
        $found = $true
        break
    }
}

if (-not $found) {
    Write-Host ""
    Write-Host "❌ 未找到性能数据文件" -ForegroundColor Red
    Write-Host ""
    Write-Host "===可能的原因===" -ForegroundColor Yellow
    Write-Host "  1. Cleanup() 函数未被调用" -ForegroundColor Gray
    Write-Host "  2. 文件写入权限不足" -ForegroundColor Gray
    Write-Host "  3. 目录不存在" -ForegroundColor Gray
    Write-Host "  4. 应用崩溃导致数据未保存" -ForegroundColor Gray
    Write-Host ""
    Write-Host "请检查日志文件: $logFile" -ForegroundColor Cyan
}

Write-Host ""
Write-Host "=== 测试完成 ===" -ForegroundColor Cyan
Write-Host ""

if ($found) {
    Write-Host "🎉 成功! 性能数据已收集" -ForegroundColor Green
    Write-Host ""
    Write-Host "下一步: 分析数据" -ForegroundColor Yellow
    Write-Host "  python analyze_performance.py oh_profiling_summary.csv" -ForegroundColor Cyan
} else {
    Write-Host "⚠ 未能收集到性能数据" -ForegroundColor Yellow
    Write-Host "请查看日志文件获取详细信息: $logFile" -ForegroundColor Cyan
}

Write-Host ""
